package com.book.controller;

import com.book.dao.BookDAO;
import com.book.dao.UserDao;
import com.book.model.Book;
import com.book.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class BookController {
    private BookDAO bookDAO = new BookDAO();

    private UserDao userDao = new UserDao();

    /**
     * 是有效用户
     *
     * @param account  账户
     * @param password 密码
     * @return boolean
     *///    账号验证逻辑
    private boolean isValidUser(String account, String password) {
        User user = userDao.getUserByAccount(account);
        if (user != null) {
            return user.getAccount().equals(account) && user.getPassword().equals(password);
        } else {
            return false;
        }

    }

    /**
     * 登录页面
     *
     * @return {@link String}
     */
    @GetMapping("/")
    public String toLoginPage() {
        return "login";
    }

    /**
     * 登录
     *
     * @param user 用户
     * @return {@link String}
     */
    @PostMapping("/login")
    public String login(User user) {
        System.out.println(user);
        if (isValidUser(user.getAccount(), user.getPassword())) {
            return "redirect:/books/list";
        } else {
            return "redirect:/";
        }
    }

    /**
     * 图书列表
     *
     * @param model 模型
     * @return {@link String}
     */
    @GetMapping("/books/list")
    public String bookList(Model model) {
        model.addAttribute("books", bookDAO.getAllBooks());
        return "books";
    }

    /**
     * 书添加
     *
     * @return {@link String}
     */
    @GetMapping("/books/add")
    public String toBookAdd() {
        return "book-add";
    }

    /**
     * 书添加
     *
     * @param book 书
     * @return {@link String}
     */
    @PostMapping("/books")
    public String bookAdd(Book book) {
        bookDAO.addBook(book);
        return "redirect:/books/list";
    }

    /**
     * 删除书
     *
     * @param id id
     * @return {@link String}
     */
    @GetMapping("/books/{id}/delete")
    public String deleteBook(@PathVariable String id) {
        bookDAO.deleteBook(Integer.parseInt(id));
        return "redirect:/books/list";
    }

    /**
     * 编辑书
     *
     * @param id    id
     * @param model 模型
     * @return {@link String}
     */
    @GetMapping("/books/{id}/edit")
    public String editBook(@PathVariable String id,Model model) {
        Book book = bookDAO.getBookById(Integer.parseInt(id));
        model.addAttribute("book", book);
        return "book-update";
    }

    /**
     * 更新保存
     *
     * @param book 书
     * @return {@link String}
     */
    @PostMapping("/books/{id}")
    public String updateSave(Book book) {
        bookDAO.updateBook(book);
        return "redirect:/books/list";
    }

    /**
     * 搜索书籍
     *
     * @param keyword 关键字
     * @param model   模型
     * @return {@link String}
     */
    @GetMapping("/books/search")
    public String searchBooks(@RequestParam("keyword") String keyword, Model model) {
        List<Book> searchResults = bookDAO.searchBooks(keyword); // 替换为实际的搜索逻辑
        model.addAttribute("books", searchResults);
        return "books"; // 替换为实际的书籍列表页面名称
    }
}
